let typeArray = ["父菜单", "子菜单"];
/**
 * 页码
 */
let pageList = [10, 50 ,100, 200, 500];

$(document).ready(function () {

    $("#role_list").datagrid({
        url: "/role/selectList",
        striped: true,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        onCheck: function (index, row) {
            let roleId = row.id;

            $("#select_list").datagrid("reload", {
                roleId: roleId
            });
            $("#menu_list").datagrid("reload", {
                roleId: roleId
            });
        },
        columns: [[
            {field: "id", title: "角色ID", hidden: true},
            {field: "name", title: "角色名", width: 100, align: "center"},
            {field: "sort", title: "图标", width: 100, align: "center"},
            {field: "description", title: "角色说明", width: 200, align: "center"},
            {field: "serviceId", title: "所属应用", width: 100, align: "center",
                formatter: function (value, rowData, rowIndex) {
                    ajaxGet("/application/selectById", {
                        id: value
                    }, function (resp) {
                        let data = resp.data;

                        $("#service_" + rowIndex).attr({
                            "src": data.href,
                            "title": data.note
                        }).html(data.name);
                    }, error);

                    return "<a id='service_" + rowIndex + "'></a>";
                }
            }
        ]]
    });

    $("#select_list").datagrid({
        url: "/role_menu/selectUnselected",
        height: 420,
        striped: true,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        selectOnCheck: false,
        pagination: true,
        pageList: pageList,
        pageSize: pageList[0],
        columns: [[
            {field: "ck", checkbox: true},
            {field: "id", title: "菜单ID", hidden: true},
            {field: "name", title: "菜单名", width: 100, align: "center"},
            {field: "type", title: "菜单类型", width: 100, align: "center",
                formatter: function (value) {
                    return "<div>" + typeArray[value] + "</div>";
                }
            },
            {field: "icon", title: "图标", width: 100, align: "center"},
            {field: "url", title: "请求路径", width: 150, align: "center"}
        ]]
    });

    $("#insert").linkbutton({
        text: "添加&gt;&gt;"
    }).on("click", function () {
        let rows = $("#select_list").datagrid("getChecked");

        if (rows && rows.length > 0) {
            let rowData = $("#role_list").datagrid("getSelected");

            if (rowData) {
                let menuIds = [];
                let roleId = rowData.id;

                for (let i = 0; i < rows.length; i++) {
                    menuIds.push(rows[i].id);
                }

                ajaxPost("/role_menu/save", {
                    roleId: roleId,
                    menuIds: JSON.stringify(menuIds)
                }, function (resp) {
                    $.messager.show({
                        title: "系统消息",
                        type: "slide",
                        msg: resp.message
                    });

                    // 重新加载表格数据
                    $("#select_list").datagrid("reload");
                    $("#menu_list").datagrid("reload");
                }, error);
            }
        } else {
            $.messager.alert("系统提示", "请选择要添加的菜单！", "warning");
        }
    });

    $("#delete").linkbutton({
        text: "&lt;&lt;移除"
    }).on("click", function () {
        let rows = $("#menu_list").datagrid("getChecked");

        if (rows && rows.length > 0) {
            let rowData = $("#role_list").datagrid("getSelected");

            if (rowData) {
                let menuIds = [];
                let roleId = rowData.id;

                for (let i = 0; i < rows.length; i++) {
                    menuIds.push(rows[i].id);
                }

                ajaxPost("/role_menu/cancel", {
                    roleId: roleId,
                    menuIds: JSON.stringify(menuIds)
                }, function (resp) {
                    $.messager.show({
                        title: "系统消息",
                        type: "slide",
                        msg: resp.message
                    });

                    // 重新加载表格数据
                    $("#select_list").datagrid("reload");
                    $("#menu_list").datagrid("reload");
                }, error);
            }
        } else {
            $.messager.alert("系统提示", "请选择要移除的菜单！", "warning");
        }
    });

    $("#menu_list").datagrid({
        url: "/role_menu/selectByRoleId",
        height: 420,
        striped: true,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        selectOnCheck: false,
        pagination: true,
        pageList: pageList,
        pageSize: pageList[0],
        columns: [[
            {field: "ck", checkbox: true},
            {field: "id", title: "菜单ID", hidden: true},
            {field: "name", title: "菜单名", width: 100, align: "center"},
            {field: "type", title: "菜单类型", width: 100, align: "center",
                formatter: function (value) {
                    return "<div>" + typeArray[value] + "</div>";
                }
            },
            {field: "icon", title: "图标", width: 100, align: "center"},
            {field: "url", title: "请求路径", width: 150, align: "center"}
        ]]
    });

});